package com.cyss.greenball.util;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.cyss.greenball.util.BaseService.ServiceListener;
import com.lianzt.commondata.AbstractCommonData;

public class BaseService {
	public static Map<Integer, AbstractCommonData> serviceList = new ConcurrentHashMap<Integer, AbstractCommonData>();
	private static Integer token = 0;
	private static ServiceThread st = new ServiceThread();
	static{
		System.out.println("service start!!");
		st.start();
	}
	
	public static Integer addService(AbstractCommonData acd) {
		serviceList.put(token, acd);
		Integer temp = token;
		token++;
		return temp;
	} 
	
	public interface ServiceListener {
		public void finishSendData(AbstractCommonData backAcd);
	}
}

class ServiceThread extends Thread {

	@Override
	public void run() {
		while (true) {
			for (Integer token : BaseService.serviceList.keySet()) {
				AbstractCommonData acd = BaseService.serviceList.get(token);
				AbstractCommonData backAcd = SystemUtil.sendCommonData(acd);
				ServiceListener sl = (ServiceListener) acd
						.getObjectValue("listener");
				sl.finishSendData(backAcd);
				BaseService.serviceList.remove(token);
			}
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

